package demo523;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.SQLException;
import java.util.Scanner;
public class TestConnection {
    // 声明一个常量：把一个变量设置为常量:final.. 改为大写

    private static final String URL = "jdbc:mysql://localhost:3306/usersDb";
    private static final String username = "root";
    private static final String password = "123456";

    public static void main(String[] args) throws SQLException {
        Connection conn = DriverManager.getConnection(URL, username, password);

        // 2.编写一条SQL语句
        String sql = "insert into users(username,password) values(?,?)";

        // 3.执行SQL语句
        PreparedStatement pstmt = conn.prepareStatement(sql); // 预编译

        // 4.给问号进行赋值
        Scanner scanner = new Scanner(System.in);
        System.out.println("请输入用户名");
        String username = scanner.next();
        System.out.println("请输入密码");
        String password = scanner.next();

        pstmt.setString(1, username);
        pstmt.setString(2, password);

        int i = pstmt.executeUpdate(); // 执行之后返回的一个整数，1
        if (i > 0) {
            System.out.println("注册成功");
        } else {
            System.out.println("注册失败");
        }
    }
}
